<?php
namespace Home\Controller;

use Home\Controller\BaseController;
use Home\Model\AssetModel;

class InstrumentLendController extends BaseController
{
    public function index() {
        $search = $_GET['search'];
        $campus = $_GET['campus'];

//        列表数据处理
        if (isset($search) && !empty($search)) {
            $where['name'] = ['like', '%' . $search . '%'];
        }
        if (isset($search) && $search === '0') {
            $where['name'] = ['like', '%' . $search . '%'];
        }
        if (isset($campus) && in_array($campus, [1,2])) {
            $where['campus'] = intval($campus);
        }
        $where['worn_out'] = 0;
//        分页
        $field = 'name,pattern,campus,userway,count(pattern) as sum,use_time';
        $result = M('asset')->field($field)->where($where)->group('pattern')->select();
        $count = count($result);
        $Page = new \Think\Page($count, 5);
        foreach ($where as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $Page->setConfig('next', '上一页');
        $Page->setConfig('prev', '下一页');
        $show = $Page->show();//

        $asset = M('asset')->field($field)->where($where)->group('pattern')->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
//        $asset=M('asset')->select();

        $this->assign('count', $count);
        $this->assign('show', $show);
        $this->assign('asset', $asset);

        var_dump($where);


//        保存默认值
        if (isset($campus) && in_array($campus, [0, 1, 2])) {
            $this->assign('campus', $campus);
        }
        if (isset($search)) {
            $this->assign('name', $search);
        }
        $this->display('teacher/InstrumentLend');
//        列表数据处理结束

        //借用信息提交  (利用session设置学年学期)
//        获取当前学年和学期的id
        $now_term=M('term_time')->field('id')->order('id desc')->limit(1)->select();
        var_dump($now_term);

    }
    public function lendForm(){
        $user_id=session('id');
        $xueyear=session('xueyear');
        $term=session('term');
        $pattern = $_POST['pattern'];
        $asset_name = $_POST['name'];

//        var_dump($pattern);
//        var_dump($asset_name);



        $lend_count = $_POST['count'];
        $lend_place = $_POST['place'];
        $lend_use = $_POST['use'];
        $reback_time=strtotime($_POST['reback_time']);

        $where['name']=$asset_name;
        $where['pattern']=$pattern;
        $where['worn_out']=0;
//       查询出相应资产id
        $data=M('asset')->field('id')->where($where)->limit($lend_count)->select();
//        $asset_name=$data[0]['name'];
        var_dump($data);
        $temp = [];
        foreach ($data as $key => $value) {
            $temp[$key] = $value['id'];
        }
        $ids = implode(',', $temp);
//        var_dump($ids);
        //查出用户基本信息
        $user=M('person')->field('user_num,name,phone')->where('id='.$user_id)->find();
        $user_num=$user['user_num'];
        $name=$user['name'];
        $phone=$user['phone'];

        $asset_data=[
            'asset_name' => $asset_name,
            'count' => $lend_count,
            'lend_time'=>time(),
            'back_time'=>$reback_time,
            'user_name'=>$name,
            'user_num'=>$user_num,
            'user_phone'=>$phone,
            'status' => 1,
            'use_address'=>$lend_place,
            'use'=>$lend_use,
            'create_time'=>time(),
            'get_status'=>1,
            'asset_ids'=>$ids,
            'pattern'=>$pattern,
            'xueyear'=>$xueyear,
            'term'=>$term,
            'user_id'=>$user_id
        ];
        $haha=M('asset_lend')->add($asset_data);
        if(isset($haha)){
            $map['id'] = array('in', $ids);
            var_dump($map);
            $dd['worn_out']=5;
            M('asset')->where($map)->save($dd);
        }
        $this->redirect('MyAsset/index');


    }

}